package com.atguigu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.atguigu.realtime.app.BaseAppV1;
import com.atguigu.realtime.bean.TradeOrderBean;
import com.atguigu.realtime.bean.TradeOrderBean;
import com.atguigu.realtime.common.Constant;
import com.atguigu.realtime.util.AtguiguUtil;
import com.atguigu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * com.atguigu.realtime.app.dws.Dws_08_DwsTradeOrderWindow
 * 交易域下单各窗口汇总表（练习）
 * 从 Kafka 订单明细主题读取数据，统计当日下单独立用户数和新增下单用户数，封装为实体类，写入 Doris。
 */
public class Dws_08_DwsTradeOrderWindow extends BaseAppV1 {
    public static void main(String[] args) {
        // 需求启动的app: 1. 预处理  2. 下单详情  3. 下单成功详情
        new Dws_08_DwsTradeOrderWindow().init(
                408,
                2,
                "Dws_08_DwsTradeOrderWindow",
                Constant.TOPIC_DWD_TRADE_ORDER_DETAIL
        );
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        stream
                .filter(
                        new FilterFunction<String>() {

                            @Override
                            public boolean filter(String jsonStr) throws Exception {
                                JSONObject jsonObj = JSON.parseObject(jsonStr);
                                String userId = jsonObj.getString("user_id");
                                String sourceTypeName = jsonObj.getString("source_type_name");
                                if (userId != null && sourceTypeName != null) {
                                    return true;
                                }
                                return false;
                            }
                        }
                )
                .map(JSON::parseObject)
                .keyBy(obj -> obj.getString("user_id"))
                //通过状态判断（去重）
                .process(new KeyedProcessFunction<String, JSONObject, TradeOrderBean>() {

                    private ValueState<String> lastOrderState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        lastOrderState = getRuntimeContext().getState(new ValueStateDescriptor<String>("lastOrderState", String.class));
                    }

                    @Override
                    public void processElement(JSONObject obj,
                                               Context ctx,
                                               Collector<TradeOrderBean> out) throws Exception {
                        String lastOrderSucDate = lastOrderState.value();
                        long ts = obj.getLong("ts") * 1000;
                        String today = AtguiguUtil.toDate(ts);


                        Long OrderUniqueUserCount = 0L;

                        // 下单成功新用户数
                        Long OrderNewUserCount = 0L;
                        // 如果这次下单日期与最后一次下单日期不相等, 则这次是今天的第一次下单
                        if (!today.equals(lastOrderSucDate)) {
                            // 这个用户的今天第一次下单成功
                            OrderUniqueUserCount = 1L;
                            lastOrderState.update(today);

                            // 是否首次下单(是否是新下单用户)
                            if (lastOrderSucDate == null) {
                                OrderNewUserCount = 1L;
                            }
                        }
                        //获取到一个下单人数就往外输出一个
                        if (OrderUniqueUserCount == 1) {
                            out.collect(new TradeOrderBean("", "",
                                    "",
                                    OrderUniqueUserCount, OrderNewUserCount,
                                    ts
                            ));
                        }
                    }
                })
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<TradeOrderBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((bean, ts) -> bean.getTs())
                )
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(
                        new ReduceFunction<TradeOrderBean>() {
                            @Override
                            public TradeOrderBean reduce(TradeOrderBean bean1,
                                                                 TradeOrderBean bean2) throws Exception {
                                bean1.setOrderNewUserCount(bean1.getOrderNewUserCount() + bean1.getOrderNewUserCount());
                                bean1.setOrderUniqueUserCount(bean1.getOrderUniqueUserCount() + bean1.getOrderUniqueUserCount());

                                return bean1;
                            }
                        },
                        //所有窗口函数:AllWindowFunction
                        new AllWindowFunction<TradeOrderBean, TradeOrderBean, TimeWindow>() {
                            @Override
                            public void apply(TimeWindow window,
                                              Iterable<TradeOrderBean> values,
                                              Collector<TradeOrderBean> out) throws Exception {
                                TradeOrderBean bean = values.iterator().next();

                                bean.setStt(AtguiguUtil.toDateTime(window.getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(window.getEnd()));

                                bean.setCurDate(AtguiguUtil.toDate(System.currentTimeMillis()));

                                out.collect(bean);
                            }
                        }
                )
                .map(bean -> {
                    SerializeConfig conf = new SerializeConfig();
                    conf.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
                    return JSON.toJSONString(bean, conf);
                })
                //.print();
                .addSink(FlinkSinkUtil.getDorisSink("gmall2022.dws_trade_order_window"));

    }
}
